package com.cloud.wms.base;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cloud.core.base.BasePresenter;
import com.cloud.core.config.ScanConfig;
import com.cloud.wms.R;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.OrderSortContract;
import com.cloud.wms.ui.sort.entity.BindInfoEntity;
import com.cloud.wms.ui.sort.presenter.OrderSortPresenter;
import com.google.android.material.tabs.TabLayout;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-07 16:09
 * @Description:
 */
public abstract class BaseFragmentActivity<P extends BasePresenter> extends BaseBarActivity<P> {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    protected String[] tabs;
    protected List<BaseViewPagerFragment> tabFragmentList = new ArrayList<>();

    @Override
    protected int getResId() {
        return R.layout.activity_common_tab;
    }

    @Override
    protected void initEvent() {
        tabs = getTabs();
        initFragment();
        vpContent.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return tabFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return tabFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });
        vpContent.setOffscreenPageLimit(tabs.length);
        //设置TabLayout和ViewPager联动
        tabLayout.setupWithViewPager(vpContent, false);
    }

    protected abstract String[] getTabs();

    protected abstract void initFragment();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ScanConfig.SCAN_CAMERA_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            /**
             * 扫码返回的数据（码格式，扫码类型，扫码内容）
             * obj.getOriginalValue();//扫码的结果
             * obj.getScanType();//二维码的格式
             * obj.getScanTypeForm();//二维码内容的类型
             */
            HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj != null && !TextUtils.isEmpty(obj.getOriginalValue())) {
                for (int i = 0; i < tabFragmentList.size(); i++) {
                    tabFragmentList.get(i).onActivityResult(requestCode, resultCode, data);
                }
            } else {
                info("扫描结果解析失败！");
            }
        }
    }
}
